package com.sahyadri.web.bean;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.ocpsoft.prettytime.PrettyTime;
import org.springframework.util.StringUtils;

import com.sahyadri.web.util.DateTimeUtil;

@Entity
@Table(name = "REVIEWS")
public class Review implements Serializable{
	
	
	private static final long serialVersionUID = 7799157978480992876L;
	@Id
	@GeneratedValue
	private long id;
	private String ownerName;
	private String phoneNumber;
	private String description;
	private String emailId;
	private boolean publish;
	private boolean subscribed;
	private int rating;
	
	@Column(columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
	@Temporal(TemporalType.TIMESTAMP)
	private Date reviewDate;
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public String getMaskPhoneNumber() {
		String regex1 = "^(\\d{2})(\\d{5})(\\d{3})$";
		String maskedPhone ="";
		
		if (StringUtils.hasText(phoneNumber)) {
			
			if(phoneNumber.length()==10){
				maskedPhone = phoneNumber.replaceFirst(regex1, "*******$3");
			}else{
				maskedPhone = "*****" + phoneNumber.substring(phoneNumber.length() - 3);
			}
		}
		return maskedPhone;
	}
	
	public String getMaskEmailId() {
		
		String maskedEmailId ="";
		
		//Pattern pattern = Pattern.compile(regex);
		if (StringUtils.hasText(emailId)) {
			
			maskedEmailId = emailId.substring(0, 1)+"*****" + emailId.substring(emailId.indexOf("@"));
			
		}
		return maskedEmailId;
	}
	
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public int getRating() {
		return rating;
	}
	public void setRating(int rating) {
		this.rating = rating;
	}
	public boolean isPublish() {
		return publish;
	}
	public void setPublish(boolean publish) {
		this.publish = publish;
	}
	public String getOwnerName() {
		return ownerName;
	}
	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	public boolean isSubscribed() {
		return subscribed;
	}
	public void setSubscribed(boolean subscribed) {
		this.subscribed = subscribed;
	}
	public Date getReviewDate() {
		return reviewDate;
	}
	public String getDisplayReviewDate() {
		
		return DateTimeUtil.changeDateFormat(reviewDate, DateTimeUtil.DEFAULT_DATE_TIME_LOCALE_FORMAT);
		 
	}
	
	public String getFormatedReviewDate(){
		PrettyTime time = new PrettyTime(reviewDate);
		return time.format(new Date()).replaceAll("from now", "ago");
	}
	public void setReviewDate(Date reviewDate) {
		this.reviewDate = reviewDate;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Review other = (Review) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	

}
